<?php
namespace Home\Model;
use Think\Model;
class QujieshequModel extends Model
{

    public function getTree()
    {
        $data = $this->select();
        return $this->_reSort($data);
    }
    private function _reSort($data, $parent_id=0, $level=0, $isClear=TRUE)
    {
        static $ret = array();
        if($isClear)
            $ret = array();
        foreach ($data as $k => $v)
        {
            if($v['parent_id'] == $parent_id)
            {
                $v['level'] = $level;
                $ret[] = $v;
                $this->_reSort($data, $v['id'], $level+1, FALSE);
            }
        }
        return $ret;
    }
    public function getChildren($id)
    {
        $data = $this->select();
        return $this->_children($data, $id);
    }
    private function _children($data, $parent_id=0, $isClear=TRUE)
    {
        static $ret = array();
        if($isClear)
            $ret = array();
        foreach ($data as $k => $v)
        {
            if($v['parent_id'] == $parent_id)
            {
                $ret[] = $v['id'];
                $this->_children($data, $v['id'], FALSE);
            }
        }
        return $ret;
    }
	// 添加前
	protected function _before_insert(&$data, $option)
	{
	}
	//在角色插入之后(因为这时已经产生了角色id)再将添加权限
	protected function _after_insert($data, $options)
	{

	}
	// 修改前
	protected function _before_update(&$data, $option)
	{

	}
	// 删除前
	protected function _before_delete($option)
	{

	}
	/************************************ 其他方法 ********************************************/
}